package com.curluin.approximateShapes;

import java.util.Vector;

import com.curluin.math.MyVector;
import com.curluin.scene.Node;

public class Rectangle extends ApproximateShape {
	
	public MyVector center;
	private float width;
	private float length;


	public Rectangle(Node node){
		width=Math.abs(node.outerSize.get(0));
		length=Math.abs(node.outerSize.get(2));
		this.center = node.getCenter();
	}
	
	/**
	 * Computes all z-coordinates of the shape lying on the specified x-coordinate,
	 * the line between two returned vectors is occupied by the shape (even=start points,uneven=end points).
	 * @param xCoordinate
	 * @return vectors, each two define a line
	 */
	private Vector<MyVector> computeSingleXScan(float xCoordinate) {
		Vector<MyVector> res = new Vector<MyVector>();
		float z1=(center.get(2)+length*0.5f);
		float z2=(center.get(2)-length*0.5f);
		MyVector result1=MyVector.makeVector3(xCoordinate,center.get(1),z1);
		MyVector result2=MyVector.makeVector3(xCoordinate,center.get(1),z2);
		res.add(result1);
		res.add(result2);
		return res;
	}
	
	@Override
	protected Vector<MyVector> initializeScanData(float sampleRate) {
		Vector<MyVector> res = new Vector<MyVector>();
		float actScan = center.get(0)-0.5f*width;
		float maxScan = center.get(0)+0.5f*width;
		while(actScan<maxScan){
			res.addAll(computeSingleXScan(actScan));
			actScan+=sampleRate;
		}
		res.addAll(computeSingleXScan(maxScan));
		this.scanData=res;
		return res;
	}



}
